#include<iostream>
#include<unistd.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include<stdio.h>
using namespace std;

int main()
{
  int id = fork();
  if(id == -1)
  {
    perror("fork()");
    exit(1);
  }
  if(id == 0)
  {
    printf("child1 begin:\n");
    execl("/bin/ls","ls","-l",NULL);
  }
  else if(id > 0)
  {
    int id2 = fork();
    if(id2 == -1)
    {
      perror("fork2()");
      exit(1);
    }
    if(id2 == 0)
    {
      printf("child2 sleep 5s\n");
      sleep(5);
      printf("child2 exit!\n");
      exit(0);
    }
    else if(id2 > 0)
    { 
      int ret1 = wait(NULL);
      printf("%d\n",ret1);
      int ret2 = wait(NULL);
      printf("%d\n",ret2);
    }
  }
  return 0;
}

